package me.pplay.playtalk.bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.pplay.playtalk.R;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Face {
	private Context mContext;
	public String[] columns;
	public List<List<String>> faceNamesList;
	public Map<String, Integer> faceMap;
	public Map<String, Bitmap> bitMaps;
	public List<List<String>> stickMap;
	private static Face face;
	
	public static Face getInstance(Context context){
		if(face == null)
			face = new Face(context);
		return face;
	}
	
	public Face(Context context){
		mContext	= context;
		columns		= context.getResources().getStringArray(R.array.tab_smile);
		intFaceNames();
		initMap(context);
	}
	
	private  void intFaceNames(){
		faceNamesList = new ArrayList<List<String>>();
		List<String> emoji = new ArrayList<String>();
		for(int i = 1; i < 64; i++){
			emoji.add(i + "");
		}
		List<String> stickers = new ArrayList<String>();
		for(int i = 1; i <= 31; i++){
			stickers.add("stickers" + i);
		}
		faceNamesList.add(emoji);
		faceNamesList.add(stickers);
		
		getStickers();
		
		
//		faceNamesList.add(stickers);
//		faceNamesList.add(new String[]{"1","2","3","4","5","6","7","8","9","10","11","12","13","emoji12","emoji13"});
		
//		faceNamesList.add(new String[]{"stickers1","stickers2","stickers3","stickers4","stickers5","stickers6","stickers7","stickers8","stickers9","stickers10","stickers11","stickers12","stickers13"});
//		faceNamesList.add(new String[]{"stickers1","stickers2","stickers3","stickers4","stickers5","stickers6","stickers7","stickers8","stickers9","stickers10","stickers11","stickers12","stickers13"});
//		faceNamesList.add(new String[]{"stickers1","stickers2","stickers3","stickers4","stickers5","stickers6","stickers7","stickers8","stickers9","stickers10","stickers11","stickers12","stickers13"});
	}

	private void getStickers(){
		stickMap = new ArrayList<List<String>>();
		bitMaps  = new HashMap<String, Bitmap>();
		try {
			AssetManager assetManager = mContext.getResources().getAssets();
			String[] fileNames	= assetManager.list("");
			for (int i = 0; i < fileNames.length; i++) {
				String fileSticker = fileNames[i];
				if(fileSticker.equals("sticker")){
					String[] filestikers = assetManager.list(fileSticker);
					for (int j = 0; j < filestikers.length; j++) {
						List<String> strs 	= new ArrayList<String>();
						String stickerPath 	= fileSticker + "/" + filestikers[j];
						String[] stikers 	= assetManager.list(stickerPath);
						for (int k = 0; k < stikers.length; k++) {
							String filename = stikers[k];
							String imgPath	= stickerPath + "/" + filename;
							strs.add(imgPath);
							Bitmap bitmap 	= BitmapFactory.decodeStream(assetManager.open(imgPath));
							if(bitmap == null) continue;
							bitMaps.put(imgPath, bitmap);
						}
						stickMap.add(strs);
					}
					break;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	

	private void initMap(Context context){
		faceMap		= new HashMap<String, Integer>();
		for(int m = 0; m < faceNamesList.size(); m++){
			String emoji = "";
			if (m == 0)
				emoji = "emoj_";
			List<String> faceNames 	= faceNamesList.get(m);
			for(int i = 0; i < faceNames.size() ; i++){
				String emoji_item 	= faceNames.get(i);
				String faceName 	= emoji + emoji_item;
				try {
					int id		= R.drawable.class.getDeclaredField(faceName).getInt(context);
					if(m == 0)
						faceMap.put("(" +emoji_item + ")", id);
					else
						faceMap.put("(" + faceName + ")", id);
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}
		}
	}

}
